import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';

import { ListResult, ListOption, APIBase } from 'app/modules/base/api-base';

import { LogService, LogInfo, ParseRouteURL } from './log.service';
import { HumanPc } from './human-pc';
import { HumanSoftware } from './human-software';
import { PcException } from './pc-exception';
import { PcUpgrade } from './pc-upgrade';
import { AreaConfig } from './area-config';
import { AreaPc } from './area-pc';
import { AreaPrivilege } from './area-privilege';
import { Usb } from './usb';
import { HttpClient } from '@angular/common/http';
import { RestrictedRecordService } from 'app/modules/usb-device/services/restricted-record.service';


@Injectable({
  providedIn: 'root',
})
export class LogListService implements Resolve<ListResult<LogInfo>> {
  constructor(private service: LogService, private http: HttpClient, private restrictedRecordService: RestrictedRecordService) {}

  async resolve(route: ActivatedRouteSnapshot) {
    const page = Number(route.queryParamMap.get('page')) || 1;
    const count = Number(route.queryParamMap.get('count')) || 10;
    const pinfo = ParseRouteURL(route);
    const options: ListOption = { page, count };
    switch (pinfo.parent) {
      case 'human':
        if (pinfo.path == 'pc') {
          this.parseHumanPcParams(route, options);
          return this.service.log<HumanPc>(route).list(options);
        } else if (pinfo.path == 'software') {
          this.parseHumanSoftwareParams(route, options);
          return this.service.log<HumanSoftware>(route).list(options);
        }
        break;
      case 'pc':
        if (pinfo.path == 'exception') {
          this.parsePcExceptionParams(route, options);
          return this.service.log<PcException>(route).list(options);
        } else if (pinfo.path == 'upgrade') {
          this.parsePcUpgradeParams(route, options);
          return this.service.log<PcUpgrade>(route).list(options);
        }
        break;
      case 'area':
        if (pinfo.path == 'config') {
          this.parseAreaConfigParams(route, options);
          return this.service.log<AreaConfig>(route).list(options);
        } else if (pinfo.path == 'pc') {
          this.parseAreaPcParams(route, options);
          return this.service.log<AreaPc>(route).list(options);
        } else if (pinfo.path == 'privilege') {
          this.parseAreaPrivilegeParams(route, options);
          return this.service.log<AreaPrivilege>(route).list(options);
        }
        break;
      case 'usb':
				if (pinfo.path === 'operation') {
          this.parseUsbLogParams(route, options);
					return this.restrictedRecordService.logs<Usb>().list(options);
				}
				break;
    }
    return null;
  }

  parseUsbLogParams(route: ActivatedRouteSnapshot, options: ListOption) {
    let keys: string[] = ['area_id', 'operation_type', 'keywords'];
    this.doParseParams(route, options, keys);
  }

  parseHumanPcParams(route: ActivatedRouteSnapshot, options: ListOption) {
    let keys: string[] = ['area_id', 'uid', '', 'keywords'];
    this.doParseParams(route, options, keys);
  }

  parseHumanSoftwareParams(route: ActivatedRouteSnapshot, options: ListOption) {
    let keys: string[] = ['area_id', 'uid', '', 'keywords'];
    this.doParseParams(route, options, keys);
  }

  parsePcExceptionParams(route: ActivatedRouteSnapshot, options: ListOption) {
    let keys: string[] = ['area_id', 'pc_id', 'error_code', 'keywords'];
    this.doParseParams(route, options, keys);
  }

  parsePcUpgradeParams(route: ActivatedRouteSnapshot, options: ListOption) {
    let keys: string[] = ['area_id', 'pc_id', 'target_id', 'status', 'keywords'];
    this.doParseParams(route, options, keys);
  }

  parseAreaConfigParams(route: ActivatedRouteSnapshot, options: ListOption) {
    let keys: string[] = ['area_id', 'operation', 'keywords'];
    this.doParseParams(route, options, keys);
  }

  parseAreaPcParams(route: ActivatedRouteSnapshot, options: ListOption) {
    let keys: string[] = ['area_id', 'has_shell', 'keywords'];
    this.doParseParams(route, options, keys);
  }

  parseAreaPrivilegeParams(route: ActivatedRouteSnapshot, options: ListOption) {
    let keys: string[] = ['area_id', 'has_shell', 'keywords'];
    this.doParseParams(route, options, keys);
  }

  doParseParams(route: ActivatedRouteSnapshot, options: ListOption, keys: string[]) {
    for (let k of keys) {
      let v = route.queryParamMap.get(k);
      if (v) {
        options[k] = v;
      }
    }
  }
}
